<?php

/**
 * m actions.
 *
 * @package    sf_sandbox
 * @subpackage m
 * @author     jason
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class mActions extends sfActions
{
    /**
     * Executes index action
     *
     */
    public function executeIndex()
    {
        $c = new Criteria();
        $c->add(TblStructurePeer::F_LEVEL, 1);
        $c->add(TblStructurePeer::F_STATUS, 'active');
        $c->addAscendingOrderByColumn(TblStructurePeer::F_SEQ_NO);
        $this->tbl_structures = TblStructurePeer::doSelect($c);
        
        $c = new Criteria();
        $c->add(TblSettingPeer::F_PARAMETER, "title");
        $tblSetting = TblSettingPeer::doSelectOne($c);
        $this->fTitle = $tblSetting->getFValue();
    }

    public function executeLandingDraft() {
        $article = $this->getRequestParameter('article');
        return $this->redirect('m/landing?isDraft=Y&article='.$article);
    }

    public function executeLanding()
    {
        $parentId = $this->getRequestParameter('article');
        $this->isDraft = $this->getRequestParameter('isDraft');

        $c = new Criteria();
        $structure = TblStructurePeer::retrieveByPK($parentId);

        $this->fMetaDescription = $structure->getFMetaDescription();
        $this->fMetaKeyword = $structure->getFMetaKeyword();
        $this->fTitle = $structure->getFTitle();
        $this->structure = $structure;

        $c = new Criteria();
        $c->add(TblStructurePeer::F_LEVEL, 2);
        $c->add(TblStructurePeer::F_STATUS, 'active');
        $c->add(TblStructurePeer::F_PARENTID, $parentId);
        $c->addAscendingOrderByColumn(TblStructurePeer::F_SEQ_NO);
        $this->tbl_structures = TblStructurePeer::doSelect($c);
    }

    public function executeArticle()
    {
        $parentId = $this->getRequestParameter('articleId');
        $this->isDraft = $this->getRequestParameter('isDraft');

        $c = new Criteria();
        $structure = TblStructurePeer::retrieveByPK($parentId);
        $this->fMetaDescription = $structure->getFMetaDescription();
        $this->fMetaKeyword = $structure->getFMetaKeyword();
        $this->fTitle = $structure->getFTitle();
        $this->structure = $structure;

        $level1Id = "";
        $level1Link = "";
        $headerLogo = "";
        if ($structure->getFLevel() == 1) {
            $level1Id = $parentId;
            $level1Link = $structure->getFLink();
            $headerLogo = $structure->getHeaderLogo();
        } else if ($structure->getFLevel() == 2) {
            $level1Id = $structure->getFParentId();

            $level1Structure = TblStructurePeer::retrieveByPK($level1Id);

            $headerLogo = $level1Structure->getHeaderLogo();
            $level1Link = $level1Structure->getFLink();
        } else if ($structure->getFLevel() == 3) {
            $level2Id = $structure->getFParentId();

            $level2Structure = TblStructurePeer::retrieveByPK($level2Id);
            $level1Id = $level2Structure->getFParentId();

            $level1Structure = TblStructurePeer::retrieveByPK($level1Id);

            $headerLogo = $level1Structure->getHeaderLogo();
            $level1Link = $level1Structure->getFLink();
        } else {
            $level3Id = $structure->getFParentId();
            $level3Structure = TblStructurePeer::retrieveByPK($level3Id);

            $level2Id = $level3Structure->getFParentId();

            $level2Structure = TblStructurePeer::retrieveByPK($level2Id);
            $level1Id = $level2Structure->getFParentId();

            $level1Structure = TblStructurePeer::retrieveByPK($level1Id);

            $headerLogo = $level1Structure->getHeaderLogo();
            $level1Link = $level1Structure->getFLink();
        }
        $this->headerLogo = $headerLogo;
        $this->level1Link = $level1Link;
        $this->level1Id = $level1Id;

        $c = new Criteria();
        $c->add(TblStructurePeer::F_STATUS, 'active');
        $c->add(TblStructurePeer::F_PARENTID, $parentId);
        $c->addAscendingOrderByColumn(TblStructurePeer::F_SEQ_NO);
        $this->tbl_structures = TblStructurePeer::doSelect($c);

        $c = new Criteria();
        $c->add(TblStructurePeer::F_STATUS, 'active');
        $c->add(TblStructurePeer::F_PARENTID, $level1Id);
        $c->addAscendingOrderByColumn(TblStructurePeer::F_SEQ_NO);
        $this->footer_menus = TblStructurePeer::doSelect($c);

        $c = new Criteria();
        $c->add(TblSettingPeer::F_PARAMETER, "share script");
        $this->tblSetting = TblSettingPeer::doSelectOne($c);
    }

    public function executeArticleDraft()
    {
        $parentId = $this->getRequestParameter('articleId');
        return $this->redirect('m/article?isDraft=Y&articleId='.$parentId);
    }

    public function executeNavigation()
    {
        $parentId = $this->getRequestParameter('articleId');

        $c = new Criteria();
        $structure = TblStructurePeer::retrieveByPK($parentId);
        $this->fMetaDescription = $structure->getFMetaDescription();
        $this->fMetaKeyword = $structure->getFMetaKeyword();
        $this->fTitle = $structure->getFTitle();
        $this->structure = $structure;

        $level1Id = "";
        $level1Link = "";
        $headerLogo = "";
        if ($structure->getFLevel() == 1) {
            $level1Id = $parentId;
            $level1Link = $structure->getFLink();
            $headerLogo = $structure->getHeaderLogo();
        } else if ($structure->getFLevel() == 2) {
            $level1Id = $structure->getFParentId();

            $level1Structure = TblStructurePeer::retrieveByPK($level1Id);

            $headerLogo = $level1Structure->getHeaderLogo();
            $level1Link = $level1Structure->getFLink();
        } else if ($structure->getFLevel() == 3) {
            $level2Id = $structure->getFParentId();

            $level2Structure = TblStructurePeer::retrieveByPK($level2Id);
            $level1Id = $level2Structure->getFParentId();

            $level1Structure = TblStructurePeer::retrieveByPK($level1Id);

            $headerLogo = $level1Structure->getHeaderLogo();
            $level1Link = $level1Structure->getFLink();
        } else {
            $level3Id = $structure->getFParentId();
            $level3Structure = TblStructurePeer::retrieveByPK($level3Id);

            $level2Id = $level3Structure->getFParentId();

            $level2Structure = TblStructurePeer::retrieveByPK($level2Id);
            $level1Id = $level2Structure->getFParentId();

            $level1Structure = TblStructurePeer::retrieveByPK($level1Id);

            $headerLogo = $level1Structure->getHeaderLogo();
            $level1Link = $level1Structure->getFLink();
        }
        $this->headerLogo = $headerLogo;
        $this->level1Link = $level1Link;
        $this->level1Id = $level1Id;

        $switchModulePageId = "";
        $switchModuleLogo = "";
        $switchModuleTitle = "";
        if ($level1Id == 1) {
            $switchModulePageId = 2;

            $switchModuleStructure = TblStructurePeer::retrieveByPK($switchModulePageId);
            $switchModuleLogo = "icon_menu_tc.png";
            $switchModuleTitle = "Switch to<br>Taylor's College";
        } else if ($level1Id == 2) {
            $switchModulePageId = 1;

            $switchModuleStructure = TblStructurePeer::retrieveByPK($switchModulePageId);
            $switchModuleLogo = "icon_menu_tu.png";
            $switchModuleTitle = "Switch to<br>Taylor's University";
        }
        $this->switchModulePageId = $switchModulePageId;
        $this->switchModuleLogo = $switchModuleLogo;
        $this->switchModuleTitle = $switchModuleTitle;

        $c = new Criteria();
        $c->add(TblStructurePeer::F_STATUS, 'active');
        $c->add(TblStructurePeer::F_PARENTID, $level1Id);
        $c->addAscendingOrderByColumn(TblStructurePeer::F_SEQ_NO);
        $this->footer_menus = TblStructurePeer::doSelect($c);
    }


    public function executeAddFavourite()
    {
        $pageId = $this->getRequestParameter('pageId');

        $pageString = $this->getUser()->getAttribute("favouriteLink");
        if ($pageString != "") {
            $pageString .= ",";
        } else {
            $pageString = "";
        }
        $pageString .= $pageId;
        $this->getUser()->setAttribute("favouriteLink", $pageString);
        return sfView::HEADER_ONLY;
    }

    public function executeClearFavourite()
    {
        $this->getUser()->getAttributeHolder()->clear();
    }

    public function executeNavigateFavorite()
    {
        $parentId = $this->getRequestParameter('level1Id');

        $c = new Criteria();
        $structure = TblStructurePeer::retrieveByPK($parentId);
        $this->fMetaDescription = $structure->getFMetaDescription();
        $this->fMetaKeyword = $structure->getFMetaKeyword();
        $this->fTitle = $structure->getFTitle();
        $this->structure = $structure;

        $level1Id = "";
        $level1Link = "";
        $headerLogo = "";
        if ($structure->getFLevel() == 1) {
            $level1Id = $parentId;
            $level1Link = $structure->getFLink();
            $headerLogo = $structure->getHeaderLogo();
        }
        $this->headerLogo = $headerLogo;
        $this->level1Link = $level1Link;
        $this->level1Id = $level1Id;

        $pageString = $this->getUser()->getAttribute("favouriteLink");
        $array = explode(',', $pageString);
        $c = new Criteria();
        $c->add(TblStructurePeer::F_STATUS, 'active');
        $c->add(TblStructurePeer::F_ID, $array, Criteria::IN);
        $footer_menus = TblStructurePeer::doSelect($c);
        $this->footer_menus = $footer_menus;
        $this->favoriteList = "T";

        $this->setTemplate('navigation');
    }

    public function executeFetchFavouriteLink()
    {
        $pageString = $this->getUser()->getAttribute("favouriteLink");

        $arr = array();
        if ($pageString != "") {
            $query = "SELECT f_id, f_link, f_name, f_level, f_parentid FROM tbl_structure WHERE f_id IN (".$pageString.")";
            //var_dump($query);
            $connection = Propel::getConnection();
            $statement = $connection->prepareStatement($query);
            $resultset = $statement->executeQuery();

            $count = 0;
            while ($resultset->next())
            {
                $resultArr = $resultset->getRow();
                $root = 0;

                if ($resultArr['f_level'] == 1) {
                    $root = $resultArr['f_id'];

                } else if ($resultArr['f_level'] == 2) {
                    $level1Id = $resultArr['f_parentid'];

                    $level1Structure = TblStructurePeer::retrieveByPK($level1Id);
                    $root = $level1Structure->getFId();

                } else if ($resultArr['f_level'] == 3) {
                    $level2Id = $resultArr['f_parentid'];

                    $level2Structure = TblStructurePeer::retrieveByPK($level2Id);
                    $level1Id = $level2Structure->getFParentId();

                    $level1Structure = TblStructurePeer::retrieveByPK($level1Id);
                    $root = $level1Structure->getFId();
                } else {
                    $level3Id = $resultArr['f_parentid'];
                    $level3Structure = TblStructurePeer::retrieveByPK($level3Id);

                    $level2Id = $level3Structure->getFParentId();

                    $level2Structure = TblStructurePeer::retrieveByPK($level2Id);
                    $level1Id = $level2Structure->getFParentId();

                    $level1Structure = TblStructurePeer::retrieveByPK($level1Id);

                    $root = $level1Structure->getFId();
                }

                $arr[] = array(
                    "f_id" =>$resultArr['f_id'],
                    "f_link" =>$resultArr['f_link'],
                    "f_name" =>$resultArr['f_name'],
                    "f_level" =>$resultArr['f_level'],
                    "root" =>$root
                );

                $count++;
            }
        }

        $output = array(
            "aaData" => $arr
        );
        echo json_encode($output);
        return sfView::HEADER_ONLY;
    }

    public function executeFetchSubmenu()
    {
        $parentId = $this->getRequestParameter('pageId');

        $c = new Criteria();
        $c->add(TblStructurePeer::F_STATUS, 'active');
        $c->add(TblStructurePeer::F_PARENTID, $parentId);
        $c->addAscendingOrderByColumn(TblStructurePeer::F_SEQ_NO);
        $tbl_structures = TblStructurePeer::doSelect($c);

        $arr = array();
        $count = 0;
        foreach ($tbl_structures as $tbl_structure):
            if($tbl_structure->getFContent()=="" && $tbl_structure->getFIcon()=="" && $tbl_structure->getFImage()=="" ) {

            } else {
                $arr[$count]["f_id"] = $tbl_structure->getFId();
                $arr[$count]["f_link"] = $tbl_structure->getFLink();
                $arr[$count]["f_name"] = $tbl_structure->getFName();
                $arr[$count]["external_link"] = $tbl_structure->getExternalLink();
                $count++;
            }
        endforeach;

        $output = array(
            "aaData" => $arr
        );
        echo json_encode($output);
        return sfView::HEADER_ONLY;
    }

}
